package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.*;
import com.qf.enums.OAExceptionEnum;
import com.qf.exception.OAException;
import com.qf.mapper.*;
import com.qf.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private LeaveMapper leaveMapper;
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private IncomeMapper incomeMapper;
    @Resource
    private ReserveCarMapper reserveCarMapper;
    @Resource
    private SealMapper sealMapper;
    @Resource
    private CardMapper  cardMapper;
    @Resource
    private ReserveMeetMapper reserveMeetMapper;

    @Override
    public PageInfo<Task> findByUsername(String username,Integer pageNum,Integer size, String startTime,String endTime,Integer taskId) {
        PageHelper.startPage(pageNum,size);
        List<Task> taskList = taskMapper.findInfoByUsername(username, startTime, endTime, taskId);
        PageInfo<Task> pageInfo = new PageInfo<>(taskList);
        return pageInfo;
    }

    @Override
    @Transient
    public int deletes(Integer[] ids) {

        return taskMapper.deletes(ids);
    }

    @Override
    public List<Task> getExcle(Integer[] ids) {
        return taskMapper.getExcle(ids);
    }

    @Override
    public Map<String,Object> detail(Integer taskId, Integer eventId) {
        Map<String,Object> map = new HashMap<String,Object>();
        if(taskId==1){
            Leave leave =leaveMapper.findInfo(taskId,eventId);
             map.put("leave",leave);
             return map;
        }else if(taskId==2){
            Business business =businessMapper.findInfo(taskId,eventId);
             map.put("business",business);
             return map;
        }else if(taskId==3){
           ReserveCar reserveCar= reserveCarMapper.findInfo(taskId,eventId);
            map.put("reserveCar",reserveCar);
            return map;
        }else if(taskId==4){
            Seal seal = sealMapper.findInfo(taskId,eventId);
            map.put("seal",seal);
            return map;
        }else if(taskId==5){
           Card card = cardMapper.findInfo(taskId,eventId);
           map.put("card",card);
           return map;
        }else if(taskId==6){
        Income income = incomeMapper.findInfo(taskId,eventId);
        map.put("income",income);
        return map;
        }else if(taskId==7){
            ReserveMeet reserveMeet = reserveMeetMapper.findInfo(taskId,eventId);
            map.put("reserveMeet",reserveMeet);
            return map;
        }
        return null;
    }

    @Override
    public PageInfo<Task> findByUserId(Integer userId, Integer pageNum, Integer size, String startTime, String endTime, Integer taskId) {
        PageHelper.startPage(pageNum,size);
        List<Task> taskList = taskMapper.findInfoByUseId(userId, startTime, endTime, taskId);
        PageInfo<Task> pageInfo = new PageInfo<>(taskList);
        return pageInfo;
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        Task param = new Task();
        param.setId(id);
        param.setStatus(status);
        param.setLastModifyTime(new Date());
        int count = taskMapper.updateByPrimaryKeySelective(param);
        if(count!=1){
            log.error("【审批操作】审批失败，id={}",id);
            throw new OAException(OAExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public List<Task> findByIds(Integer[] ids) {
        return taskMapper.findTaskByIds(ids);
    }
}
